package com.zhxh.nzpos.utils;



import android.app.ProgressDialog;
import android.content.Context;
import android.content.res.Configuration;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Utility class for commons views required by activities
 * 
 * @author kevin
 */
public class ViewUtil {

    /**
     * handle to a progress dialog used by all the activities
     */
    private static ProgressDialog mProgressDialog;

    private static final String LOG_TAG = "ViewUtil";

    /**
     * show ProgressDialog
     * 
     * @param context
     *            a {@link android.content.Context} object.
     * @param startMsg
     *            a {@link java.lang.String} object.
     */
    public static void showProgressDialog(Context context, String startMsg) {
        showProgressDialog(context, startMsg, true);
    }

    /**
     * show ProgressDialog
     * 
     * @param context
     *            a {@link android.content.Context} object.
     * @param startMsg
     *            a {@link java.lang.String} object.
     * @param indeterminate
     *            a boolean.
     */
    public static void showProgressDialog(Context context, String startMsg, boolean indeterminate) {
        mProgressDialog = new ProgressDialog(context);
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        mProgressDialog.setCancelable(indeterminate);
        // mProgressDialog.setMax(100);
        mProgressDialog.setMessage(startMsg);
        mProgressDialog.show();
    }

    /**
     * get ProgressDialog
     * 
     * @param context
     *            a {@link android.content.Context} object.
     * @param startMsg
     *            a {@link java.lang.String} object.
     * @param indeterminate
     *            a boolean.
     * @return a {@link android.app.ProgressDialog} object.
     */
    public static ProgressDialog getProgressDialog(Context context, String startMsg,
                                    boolean indeterminate) {
        ProgressDialog progressDialog = new ProgressDialog(context);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setCancelable(indeterminate);
        // mProgressDialog.setMax(100);
        progressDialog.setMessage(startMsg);
        return progressDialog;
        // mProgressDialog.show();
    }

    /**
     * cancel ProgressBar
     */
    public static void cancelProgressDialog() {
        if (mProgressDialog != null && mProgressDialog.isShowing())
            mProgressDialog.cancel();
    }

    /**
     * helper method to display a toast specified by the resource id (strings.xml)
     * 
     * @param context
     *            a {@link android.content.Context} object.
     * @param resid
     *            a int.
     */
    public static void makeToast(Context context, int resid) {
        Toast toast = Toast.makeText(context, resid, Toast.LENGTH_LONG);
        toast.show();
    }

    /**
     * helper method to display a toast specified by the resource id (strings.xml)
     * 
     * @param context
     *            a {@link android.content.Context} object.
     * @param str
     *            a {@link java.lang.CharSequence} object.
     */
    public static void makeToast(Context context, CharSequence str) {
        Toast toast = Toast.makeText(context, str, Toast.LENGTH_LONG);
        toast.show();
    }

    /**
     * showFormattedToast
     * 
     * @param context
     *            a {@link android.content.Context} object.
     * @param id
     *            a int.
     * @param args
     *            a {@link java.lang.Object} object.
     */
    public static void showFormattedToast(Context context, int id, Object... args) {
        Toast.makeText(context, String.format(context.getString(id), args), Toast.LENGTH_LONG).show();
    }

    /**
     * dismissVirtualKeyboard
     * 
     * @param context
     *            a {@link android.content.Context} object.
     * @param view
     *            a {@link android.view.View} object.
     */
    public static void dismissVirtualKeyboard(Context context, View view) {

        InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        // if(inputManager.isActive(view))
        inputManager.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);
        // inputManager.showSoftInput(ourCanvasInstance, 0);
        // inputManager.hideSoftInputFromInputMethod(this, 0);
        // && inputManager.isFullscreenMode()
        // if(inputManager.isActive())
        // {
        // inputManager.toggleSoftInput(0, 0);
        // }
    }

/*    public static boolean isHoneycomb() {
        // Can use static final constants like HONEYCOMB, declared in later versions
        // of the OS since they are inlined at compile time. This is guaranteed behavior.
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB;
    }*/

    public static boolean isTablet(Context context) {
        return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;
    }

/*    public static boolean isHoneycombTablet(Context context) {
        return isHoneycomb() && isTablet(context);
    }*/
    
	/**
	 * Create toast notification with an image in front of text.
	 * 
	 * @param context
	 * @param imageResId
	 *            resource ID for image
	 * 
	 * @return toast with image, empty text and {@link Toast#LENGTH_SHORT}
	 * 
	 * @see #makeImageToast(Context, int, int, int)
	 */
	public static Toast makeImageToast(Context context, int imageResId) {
		return makeImageToast(context, imageResId, "", Toast.LENGTH_SHORT);
	}
	
	/**
	 * Create toast notification with an image in front of text.
	 * 
	 * @param context
	 * @param imageResId
	 *            resource ID for image
	 * @param textResId
	 *            resource ID for text
	 * @param length
	 *            {@link Toast#LENGTH_LONG} or {@link Toast#LENGTH_SHORT}
	 * 
	 * @return toast with image
	 * 
	 * @see #makeImageToast(Context, int, int, int)
	 */
	public static Toast makeImageToast(Context context, int imageResId, int textResId, int
			length) {
		return makeImageToast(context, imageResId, context.getString(textResId), length);
	}
	
	/**
	 * Create toast notification with an image in front of text.<br>
	 * <br>
	 * Since it's not that easy to recreate the original toast layout, we take the original and
	 * modify it. This is a really dirty hack which places an image view into an inflated toast
	 * layout.<br>
	 * However, it won't crash on implementation changes of android's toast layout (which will
	 * probably not happen). If so you will get an ordinary toast.
	 * 
	 * @param context
	 * @param imageResId
	 *            resource ID for image
	 * @param text
	 *            text to use
	 * @param length
	 *            {@link Toast#LENGTH_LONG} or {@link Toast#LENGTH_SHORT}
	 * 
	 * @return toast with image
	 */
	public static Toast makeImageToast(Context context, int imageResId, CharSequence text,
			int length) {
		Toast toast = Toast.makeText(context, text, length);
		
		View rootView = toast.getView();
		LinearLayout linearLayout = null;
		View messageTextView = null;
		
		// check (expected) toast layout
		if (rootView instanceof LinearLayout) {
			linearLayout = (LinearLayout) rootView;
			
			if (linearLayout.getChildCount() == 1) {
				View child = linearLayout.getChildAt(0);
				
				if (child instanceof TextView) {
					messageTextView = (TextView) child;
					
					if (!(messageTextView.getLayoutParams() instanceof LinearLayout.LayoutParams)) {
						messageTextView = null;
					}
				}
			}
		}
		
		// cancel modification because toast layout is not what we expected
		if (linearLayout == null || messageTextView == null) {
			Log.w(LOG_TAG,"failed to create image toast layout, using usual toast");
			return toast;
		}
		
		ViewGroup.LayoutParams textParams = messageTextView.getLayoutParams();
		((LinearLayout.LayoutParams) textParams).gravity = Gravity.CENTER_VERTICAL;
		
		// convert dip dimension
		int imageSize = DisplayUtil.dipToPx(context, 25);
		int imageMargin =  DisplayUtil.dipToPx(context, 15);
		
		// setup image view layout parameters
		LinearLayout.LayoutParams imageParams = new LinearLayout.LayoutParams(imageSize, imageSize);
		imageParams.setMargins(0, 0, imageMargin, 0);
		imageParams.gravity = Gravity.CENTER_VERTICAL;
		
		// setup image view
		ImageView imageView = new ImageView(context);
		imageView.setImageResource(imageResId);
		imageView.setLayoutParams(imageParams);
		
		// modify root layout
		linearLayout.setOrientation(LinearLayout.HORIZONTAL);
		linearLayout.addView(imageView, 0);
		
		return toast;
	}
	

}
